home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / pascal / joystck.com / JOYSTICK.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1989-11-14  |  2.5 KB  |  118 lines

  1. unit Joystick;
  2. interface
  3.  uses CRT;
  4.  
  5.  { Joystick interface for Turbo Pascal V. 4.0 and above
  6.    Public Domain, November 1989 by
  7.    JonSoft Technologies Inc.
  8.    (C) 1989 JonSoft Technologies Inc. }
  9.  
  10. const
  11.  CentX : byte=80;
  12.  CentY : byte=40;
  13.  Joyst : boolean=true;
  14.  
  15. procedure FastInitJS;
  16. procedure BetterInitJS( Range : byte );
  17. function JoyX : byte;
  18. function JoyY : byte;
  19. function Button1 : byte;
  20. function Button2 : byte;
  21. function Horiz : shortint;
  22. function Vert : shortint;
  23.  
  24.  
  25. implementation
  26.  
  27. const
  28.  RangeXM : byte=25;
  29.  RangeYM : byte=20;
  30.  RangeXP : byte=25;
  31.  RangeYP : byte=25;
  32.  
  33. function JoyX : byte;
  34. var
  35.  X : word;
  36. begin
  37.  X:=0;
  38.  Port[$201]:=$ff;
  39.  While Port[$201] and $1=1 do Inc(X);
  40.  JoyX:=X;
  41. end;
  42.  
  43. function JoyY : byte;
  44. var
  45.  Y : word;
  46. begin
  47.  Y:=0;
  48.  Port[$201]:=$0;
  49.  While Port[$201] and $2=2 do Inc(Y);
  50.  JoyY:=Y;
  51. end;
  52.  
  53. procedure FastInitJs;
  54. begin
  55.  CentX:=JoyX;
  56.  CentY:=JoyY;
  57. end;
  58.  
  59. function Button1 : byte;
  60. begin
  61.  Button1:=((Port[$201] and $10) xor $10) shr 4;
  62. end;
  63.  
  64. function Button2 : byte;
  65. begin
  66.  Button2:=((Port[$201] and $20) xor $20) shr 5;
  67. end;
  68.  
  69. procedure BetterInitJs(Range : byte);
  70. var
  71.  ch : char;
  72.  UprJoyX, UprJoyY, CentrJoyX, CentrJoyY, LowrJoyX, LowrJoyY : byte;
  73.  
  74. begin
  75.  writeln('Are you using a joystick? (Button = yes, RETURN = no)');
  76.  Repeat
  77.   If Button1+Button2>0 then Joyst:=true;
  78.   If Keypressed then Joyst:=false;
  79.  Until (Button1+Button2>0) or Keypressed;
  80.  If Joyst=true then begin
  81.   repeat until Button1+Button2=0;
  82.   Writeln('Move joystick to UPPER RIGHT corner and press a button.');
  83.   repeat until Button1+Button2>0;
  84.   UprJoyX:=JoyX;
  85.   UprJoyY:=JoyY;
  86.   repeat until Button1+Button2=0;
  87.   Writeln('Move joystick to CENTER and press a button.');
  88.   repeat until Button1+Button2>0;
  89.   CentrJoyX:=JoyX;
  90.   CentrJoyY:=JoyY;
  91.   CentX:=CentrJoyX;
  92.   CentY:=CentrJoyY;
  93.   repeat until Button1+Button2=0;
  94.   Writeln('Move joystick to LOWER LEFT CORNER and press a button.');
  95.   repeat until Button1+Button2>0;
  96.   LowrJoyX:=JoyX;
  97.   LowrJoyY:=JoyY;
  98.   RangeXM:=(CentrJoyX-UprJoyX) div Range;
  99.   RangeXP:=(LowrJoyX-CentrJoyX) div Range;
  100.   RangeYM:=(CentrJoyY-UprJoyY) div Range;
  101.   RangeYP:=(LowrJoyY-CentrJoyY) div Range;
  102.  end;
  103. end;
  104.  
  105. function Horiz : shortint;
  106. begin
  107.  If JoyX<CentX-RangeXM then Horiz:=-1
  108.   else if JoyX>CentX+RangeXP then Horiz:=1
  109.    else Horiz:=0;
  110. end;
  111.  
  112. function Vert : shortint;
  113. begin
  114.  If JoyY<CentY-RangeYM then Vert:=-1
  115.   else if JoyY>CentY+RangeYP then Vert:=1
  116.    else Vert:=0;
  117. end;
  118. end.